package com.young.ysepay.service.impl;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.young.ysepay.domain.YsWechatpayReceiptDO;
import com.young.ysepay.domain.YsWithdrawalReceiptDO;
import com.young.ysepay.mq.YsReturnMQConfig;
import com.young.ysepay.service.*;
import com.young.ysepay.utils.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * describe:
 *
 * @author zhaopengjun
 * @date 2019/6/10 17:50
 */
@Service
public class YsAsyncPaymentServiceImpl implements YsAsyncPaymentService {
    @Autowired
    YsVerifyJsonSignService ysVerifyJsonSignService;
    @Autowired
    YsNotifyRegistryService ysNotifyRegistryService;
    @Autowired(required = false)
    private Producer producer;
    @Autowired
    private YsWithdrawalReceiptService ysWithdrawalReceiptService;
    private static Logger log = LoggerFactory.getLogger(YsAsyncPaymentServiceImpl.class);
    @Override
    public void asyncPayment(Map<String,String> paramMap, HttpServletResponse response) {
        PrintWriter writer =null;
        try {
            saveAsyncWithdrawal(paramMap);
            Message message = null;
            writer = response.getWriter();
            boolean verifyResult = ysVerifyJsonSignService.verifySign(paramMap);
            log.error("银盛异步待打款回執信息"+verifyResult);
            // 状态
            if (verifyResult) {// 验证成功
                message = new Message(YsReturnMQConfig.SETTLETOPIC, YsReturnMQConfig.YSSPLITRECEIPT_TAG, JSONUtils.objectToJson(paramMap).getBytes());
                // 发送Mq
                producer.send(message);
                log.error("银盛异步待打款回執发送消息"+new String(message.getBody()));
                writer.print("success");

            } else {// 验证失败
                log.error("银盛异步待打款回執错误消息"+new String(message.getBody()));
                writer.print("fall");
            }
        } catch (Exception e) {
            log.error("银盛异步待打款回执接口调用失败！",e.getMessage());
            e.printStackTrace();
        }finally {
            if (writer != null){
                writer.close();
            }
        }


    }

    private void saveAsyncWithdrawal(Map<String,String> paramMap) {

        YsWithdrawalReceiptDO ysWithdrawalReceiptDO = new YsWithdrawalReceiptDO();
        try {
            String out_trade_no = paramMap.get("out_trade_no");
            String trade_status = paramMap.get("trade_status");
            String trade_status_description = paramMap.get("trade_status_description");
            String total_amount = paramMap.get("total_amount");
            String account_date = paramMap.get("account_date");
            String trade_no = paramMap.get("trade_no");
            String fee = paramMap.get("fee");
            String partner_fee = paramMap.get("partner_fee");
            String payee_fee = paramMap.get("payee_fee");
            String payer_fee = paramMap.get("payer_fee");
            String notify_type = paramMap.get("notify_type");
            String notify_time = paramMap.get("notify_time");
            String sign_type = paramMap.get("sign_type");
            String sign = paramMap.get("sign");

            ysWithdrawalReceiptDO.setOutTradeNo(out_trade_no);
            ysWithdrawalReceiptDO.setAccountDate(account_date);
            ysWithdrawalReceiptDO.setSign(sign);
            ysWithdrawalReceiptDO.setNotifyTime(notify_time);
            ysWithdrawalReceiptDO.setNotifyType(notify_type);
            ysWithdrawalReceiptDO.setTotalAmount(new BigDecimal(total_amount));
            ysWithdrawalReceiptDO.setTradeNo(trade_no);
            ysWithdrawalReceiptDO.setTradeStatus(trade_status);
            ysWithdrawalReceiptDO.setResponseType("2");
            ysWithdrawalReceiptDO.setIsDelete("1");
            ysWithdrawalReceiptDO.setGmtModified(new Date());
            ysWithdrawalReceiptDO.setGmtCreate(new Date());
            ysWithdrawalReceiptDO.setSignType(sign_type);
            ysWithdrawalReceiptDO.setPayerFee(new BigDecimal(payer_fee));
            ysWithdrawalReceiptDO.setPayeeFee(new BigDecimal(payee_fee));
            ysWithdrawalReceiptDO.setPartnerFee(new BigDecimal(partner_fee));
            ysWithdrawalReceiptDO.setFee(new BigDecimal(fee));
            ysWithdrawalReceiptDO.setTradeStatusDescription(trade_status_description);
            ysWithdrawalReceiptService.save(ysWithdrawalReceiptDO);
        }catch (Exception e){
            log.error("提现异步回执日志记录保存失败"+ysWithdrawalReceiptDO.toString());
        }

    }
}
